package Stragos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Stragos.excepcion.DAOExcepcion;
import Stragos.modelo.Reserva;
import Stragos.modelo.Sala;
import Stragos.util.ConexionBD;

public class ReservaDAO extends BaseDAO {

	public void insertar(Reserva vo) throws DAOExcepcion {
		System.out.println("ReservaDAO: insertar(Reserva vo)");
		String query = "INSERT INTO reserva(fecha, descripcion, elog, idSala, idCliente) VALUES (?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getFecha());
			stmt.setString(2, vo.getDescripcion());
			stmt.setBoolean(3, vo.isElog());
			stmt.setInt(4, vo.getIdSala());
			stmt.setInt(5, vo.getIdCliente());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}

	}
	
	public Reserva obtener(int idReserva) throws DAOExcepcion {
		Reserva vo = new Reserva();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select fecha, descripcion, from sala where idReserva=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idReserva);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setFecha(rs.getString(1));
				vo.setDescripcion(rs.getString(2));
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idReserva) throws DAOExcepcion {
		String query = "delete from reserva WHERE idreserva=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idReserva);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public ArrayList<Reserva> listar() throws DAOExcepcion {
		ArrayList<Reserva> c = new ArrayList<Reserva>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idreserva,fecha,descripcion,elog,idsala,idcliente from reserva  order by fecha desc;";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Reserva vo = new Reserva();
				vo.setIdReserva(rs.getInt("idreserva"));
				vo.setFecha(rs.getString("fecha"));
				vo.setDescripcion(rs.getString("descripcion"));				
				vo.setElog(rs.getBoolean("elog"));
				vo.setIdSala(rs.getInt("idSala"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
}
